package cn.com.bsoft.cloud.utils;

import cn.com.bsoft.immunity.utils.HttpUtilsKt;
import cn.net.xyan.blossom.kotlin.JsonExtKt;
import com.alibaba.fastjson.JSONObject;
import kotlin.jvm.internal.Reflection;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ls
 * @Date: 2020/3/7 17:30
 */
public class ESignUtil {

    private static String url = "https://bohemia.bshcn.com.cn/esign/";
    private static String urlpng = "http://192.168.30.201/pay/redirect/display.png";
    /**
     * 注册盖章人员信息
     */

    public static void main(String array[]) throws Exception {
        List<JSONObject> rspList = null;
        JSONObject rspJSONObject = null;
        String rsp = "";
//        JSONObject obj = new JSONObject();
//            obj.put("domain", "yangpu.doctor");
//            obj.put("loginName", "demoDoctor");
//        obj.put("idNo", "310107XXXXXXXXXXXX");
//            obj.put("accountId", "2A029C5E35FE43F39EDD59A6F1E33194");
//        obj.put("hospitalCode", "XXXXXXXX");
//        obj.put("hospitalName", "杨浦区中心医院");
        JSONObject obj = new JSONObject();

//        {
//            "domain": "xuhui",
//                "outputType": "image",
//                "prescriptionCode": "2c918082705c14de0170b46d31ae0004",
//                "hospitalCode": "42483500100"
//        }
        rspJSONObject = prescriptionDisplay(obj);

        System.out.println(rsp.toString());
    }



    /**
     * * 医院代码、处方id
     * * domain  xuhui.doctor
     * loginName  医生身份证号
     * signType  1 医生  2药剂师
     *
     * @param obj
     * @return
     * @throws Exception
     */
    public static JSONObject prescriptionSign(JSONObject obj) throws Exception {
/*        if(obj==null){
            obj=new JSONObject();
        }
        obj.put("hospitalCode", "42483500100");
        obj.put("prescriptionCode", "e2b57951-bb13-4e2e-8d0d-502a317e8cb6");
        obj.put("domain", "xuhui.doctor");
        obj.put("loginName", "demoDoctor");
        obj.put("signType", 1);*/
        JSONObject rsp = null;
        try {
            rsp = HttpUtilsKt.postJson(new URL(url + "/prescription/sign"), obj, JsonExtKt.getJavaType(Reflection.getOrCreateKotlinClass(JSONObject.class)));
        } catch (MalformedURLException e) {
            throw new Exception("签章错误" + e.getMessage());
        }
        return rsp;
    }

    /**
     * 医院代码、处方id
     * domain  xuhui.doctor
     * outputType  输出文件类型，image、pdf等
     *
     * @param obj
     * @return
     */
    public static JSONObject prescriptionDisplay(JSONObject obj) throws Exception {
//        obj=new JSONObject();
//        obj.put("hospitalCode", "42483500100");
//        obj.put("prescriptionCode", "code001");
//        obj.put("domain", "xuhui.doctor");
//        obj.put("outputType", "image");
        JSONObject rsp = new JSONObject();
        String rsStr = "";
        try {
            rsp.put("code", "1");
            rsp.put("msg", "获取成功");
            rsStr = urlpng+"?domain=" + obj.get("domain").toString() + "&hospitalCode=" + obj.get("hospitalCode").toString() + "&prescriptionCode=" + obj.get("prescriptionCode").toString() + "&outputType=" + obj.get("outputType").toString();
            rsp.put("result", rsStr);
        } catch (Exception e) {
            rsp.put("code", "0");
            rsp.put("msg", "获取失败");
            rsStr = "下载错误";
            rsp.put("result", rsStr);
        }
        return rsp;
    }

    public static Map<String, Object> getIdentifyNoInfo(String IdentifyNo)
            throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String year = IdentifyNo.substring(6).substring(0, 4);// 得到年份
        String yue = IdentifyNo.substring(10).substring(0, 2);// 得到月份
        // String day=CardCode.substring(12).substring(0,2);//得到日
        String sex;
        if (Integer.parseInt(IdentifyNo.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
            sex = "女";
        } else {
            sex = "男";
        }
        Date date = new Date();// 得到当前的系统时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String fyear = format.format(date).substring(0, 4);// 当前年份
        String fyue = format.format(date).substring(5, 7);// 月份
        // String fday=format.format(date).substring(8,10);
        int age = 0;
        if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生
            age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
        } else {// 当前用户还没过生
            age = Integer.parseInt(fyear) - Integer.parseInt(year);
        }
        map.put("sex", sex);
        map.put("age", age);
        return map;
    }


    public static JSONObject prescriptionSubmit(JSONObject obj) throws MalformedURLException {
       /* JSONArray array = new JSONArray();
        if(obj==null){
            obj=new JSONObject();
        }
        obj.put("domain", "xuhui.doctor");
        obj.put("hospitalCode", "42483500100");
        obj.put("hospitalName", "杨浦中心医院");
        obj.put("patientType", "自费");
        obj.put("prescriptionCode", "e2b57951-bb13-4e2e-8d0d-502a317e8cb6");
        obj.put("patientName", "测试病人");
        obj.put("patientGender", "男");
        obj.put("patientAge", "37岁");
        obj.put("patientIdNo", "310107XXXXXXXXXXXX");
        obj.put("patientId", "N1234567");
        obj.put("patientMobile", "135XXXXXXXX");
        obj.put("department", "全科");
        obj.put("clinicalDiagnosis", "I型糖尿病");

        JSONObject obj1 = new JSONObject();
//        JSONObject obj2 = new JSONObject();
        obj1.put("itemCode", "itemCode001");
        obj1.put("itemName", "头孢");
        obj1.put("spec", "10片/包*3包/箱*2箱/盒");
        obj1.put("amount", 1);
        obj1.put("amountUnit", "盒");
        obj1.put("usageAmount", 1);
        obj1.put("usageUnit", "片");
        obj1.put("usageRoute", "口服");
        obj1.put("frep", "bid");
        obj1.put("usageTime", "3");
        obj1.put("usageTimeUnit", "天");


        array.add(obj1);
//        array.add(obj2);
        obj.put("items", array);*/
        JSONObject rsp = HttpUtilsKt.postJson(new URL(url + "/prescription/submit"), obj, JsonExtKt.getJavaType(Reflection.getOrCreateKotlinClass(JSONObject.class)));
        return rsp;
    }
}
